<template>
  <div>
    <transition name="down">
      <div
        v-show="isVisable"
        class="min-w-[420px] fixed top-[20px] left-[50%] translate-x-[-50%] z-50 flex items-center px-3 py-1.5 rounded-sm border cursor-pointer"
        :class="styles[type].containerClass"
      >
        <m-svg-icon
          :name="styles[type].icon"
          class="w-1.5 h-1.5 mr-1.5"
          :fillClass="styles[type].fillClass"
        ></m-svg-icon>
        <span class="text-sm" :class="styles[type].textClass">{{
          content
        }}</span>
      </div>
    </transition>
  </div>
</template>
<script>
const SUCCESS = 'success'
const WARN = 'warn'
const ERROR = 'error'
const typeEnums = [SUCCESS, WARN, ERROR]
</script>
<script setup>
import mSvgIcon from '../svg-icon/index.vue'
import { ref, onMounted } from 'vue'
const props = defineProps({
  type: {
    type: String,
    required: true,
    validator(value) {
      const result = typeEnums.includes(value)
      if (!result) {
        console.warn(`type must be one of ${typeEnums.join(',')}`)
      }
      return result
    }
  },
  content: {
    type: String,
    required: true
  },
  duration: {
    type: Number,
    default: 3000
  },
  // 关闭时的回调
  destory: {
    type: Function
  }
})
// 样式数据表
const styles = {
  warn: {
    icon: 'warn',
    fillClass: 'fill-warn-300',
    textClass: 'text-warn-300',
    containerClass:
      'bg-warn-100 border-warn-200 hover:shadow-lg hover:shadow-warn-100'
  },
  error: {
    icon: 'error',
    fillClass: 'fill-error-300',
    textClass: 'text-error-300',
    containerClass:
      'bg-error-100 border-error-200 hover:shadow-lg hover:shadow-error-100'
  },
  success: {
    icon: 'success',
    fillClass: 'fill-success-300',
    textClass: 'text-success-300',
    containerClass:
      'bg-success-100 border-success-200 hover:shadow-lg hover:shadow-success-100'
  }
}
const isVisable = ref(false)
onMounted(() => {
  isVisable.value = true
  setTimeout(() => {
    isVisable.value = false
    if (props.destory) {
      props.destory()
    }
  }, props.duration)
})
</script>
<style lang="scss" scoped>
.down-enter-active,
.down-leave-active {
  transition: all 0.3s ease;
}
.down-enter-from,
.down-leave-to {
  opacity: 0;
  transform: translate3d(-50%, -100px, 0);
}
</style>
